package cn.zyq.ykb.service.config.impl;

import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.config.ConfigInfoMapper;
import cn.zyq.ykb.beans.config.ConfigInfo;
import cn.zyq.ykb.service.config.ConfigInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 基础信息配置Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-22
 */
@Service
public class ConfigInfoServiceImpl  extends BaseService implements ConfigInfoService {

    @Autowired
    private ConfigInfoMapper configInfoMapper;

    /**
     * 查询基础信息配置
     *
     * @param configInfoID 基础信息配置主键
     * @return 基础信息配置
     */
    @Override
    public ConfigInfo selectConfigInfoByConfigInfoID(Integer configInfoID) {
        ConfigInfo configInfo = configInfoMapper.selectByPrimaryKey(configInfoID);
        if (configInfo.getDeleted() == 1) {
            return null;
        }
        return configInfo;
    }

    /**
     * 查询基础信息配置
     *
     * @param serviceID 业务ID
     * @return 基础信息配置
     */
    @Override
    public ConfigInfo selectConfigInfoByServiceID(String serviceID) {
        ConfigInfo param = new ConfigInfo();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.configInfoMapper.selectOne(param);
    }

    /**
     * 查询基础信息配置列表
     *
     * @param pageParam
     * @return 基础信息配置
     */
    @Override
    public PageInfo<ConfigInfo> selectConfigInfoList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<ConfigInfo> list = this.configInfoMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询基础信息配置列表
     *
     * @param param
     * @return 基础信息配置
     */
    @Override
    public List<ConfigInfo> selectConfigInfoList(ConfigInfo param) {
        param.setDeleted(0);
        return this.configInfoMapper.select(param);
    }
    /**
     * 新增基础信息配置
     *
     * @param param 基础信息配置
     * @return 结果
     */
    @Override
    public int insertConfigInfo(ConfigInfo param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.configInfoMapper.insertSelective(param);
    }

    /**
     * 修改基础信息配置
     *
     * @param param 基础信息配置
     * @return 结果
     */
    @Override
    public int updateConfigInfo(ConfigInfo param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.configInfoMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除基础信息配置
     *
     * @param configInfoIDs 需要删除的基础信息配置主键
     * @return 结果
     */
    @Override
    public int deleteConfigInfoByConfigInfoIDs(Integer[] configInfoIDs) {
        int i = 0;
        for (Integer configInfoID : configInfoIDs) {
            i += this.deleteConfigInfoByConfigInfoID(configInfoID);
        }
        return i;
    }

    /**
     * 删除基础信息配置信息
     *
     * @param configInfoID 基础信息配置主键
     * @return 结果
     */
    @Override
    public int deleteConfigInfoByConfigInfoID(Integer configInfoID) {
        ConfigInfo configInfo = this.selectConfigInfoByConfigInfoID(configInfoID);
        if (configInfo == null) {
            return 0;
        } else {
            ConfigInfo storeInfo = new ConfigInfo();
            storeInfo.setConfigInfoID(configInfoID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.configInfoMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
